package com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.bizactions;

import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.condition.EntityFilter;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.entity.ICefData;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IValuesContainer;

import java.util.ArrayList;
import java.util.List;

public class SbssSbtzChangeStatusMgrAction extends AbstractManagerAction<VoidActionResult> {
    private String ztName;
	private String beId;
	private String changeUser;
	private String valueFrom;
	private String jlz;
	public SbssSbtzChangeStatusMgrAction(IBEManagerContext managerContext,
			String ztName, String beId, String changeUser, String valueFrom,
			String jlz) {
		super(managerContext);
		this.ztName = ztName;
		this.beId = beId;
		this.changeUser = changeUser;
		this.valueFrom = valueFrom;
		this.jlz = jlz;
	}

    @Override
    public void execute() {
        if (!jlz.equals("pass")){
            return;
        }

        IStandardLcp tzlcp = getBEManagerContext().getLcp("com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.SbssSbtz");
        EntityFilter filter = new EntityFilter();
        ArrayList<FilterCondition> filters = new ArrayList<>(
                FilterCondition.builder()
                        .andEqual("id", beId)
                        .build()
        );
        filter.setFilterConditions(filters);
        List<IEntityData> query = tzlcp.query(filter);
        // 找不到实体则结束
        if (query.isEmpty()){
            return;
        }
        IEntityData entity = query.get(0);
        String oldZt = ((IValuesContainer) entity.getValue("zt")).getValue("zt_ztmc").toString();

        IStandardLcp lcp = getBEManagerContext().getLcp("com.inspur.gs.sbssdl.database.datadict.datadict.back.SBZT");
        filter = new EntityFilter();
        filters = new ArrayList<>(
                FilterCondition.builder()
                        .andEqual("ztmc", ztName)
                        .build()
        );
        filter.setFilterConditions(filters);
        query = lcp.query(filter);
        // 找不到相应状态则结束
        if (query.isEmpty()){
            return;
        }
        IEntityData ztEntity = query.get(0);

        ModifyChangeDetail modify = new ModifyChangeDetail(entity.getID());
        AssociationInfo ztInfo = new AssociationInfo();
        ztInfo.setValue("zt",ztEntity.getID());
        ztInfo.setValue("zt_ztmc",ztEntity.getValue("ztmc"));
        modify.getPropertyChanges().put("zt", ztInfo);

        // 修改BE变量
        ValueObjModifyChangeDetail bemodify = new ValueObjModifyChangeDetail();
        bemodify.setItem("DataName","zt");
        bemodify.setItem("DataComm","使用状态");
        bemodify.setItem("oldValue",oldZt);
        bemodify.setItem("updValue",ztEntity.getValue("ztmc"));
        bemodify.setItem("valueFrom",valueFrom);
        bemodify.setItem("changeUser",changeUser);
        tzlcp.modifyVariable(bemodify);

        tzlcp.modify(modify);
        tzlcp.save();
    }

    private IBusinessEntity getEntity() {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(beId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}